package com.copa.ums.modules.ums.controller;

import com.copa.ums.common.api.CommonPage;
import com.copa.ums.common.vo.ResponseVo;
import com.copa.ums.modules.ums.dto.UmsUserLoginDto;
import com.copa.ums.modules.ums.dto.UmsUserDto;
import com.copa.ums.modules.ums.dto.UpdateUserPasswordDto;
import com.copa.ums.modules.ums.model.UmsRole;
import com.copa.ums.modules.ums.model.UmsUser;
import com.copa.ums.modules.ums.service.UmsUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author copa
 * @since 2022-10-31
 */
@RestController
@Api(tags = "用户管理")
@Tag(name = "用户管理",description = "用户管理")
@RequestMapping("user")
public class UmsUserController {

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;

    @Resource
    private UmsUserService umsUserService;

    @ApiOperation("用户注册")
    @PostMapping("register")
    public ResponseVo<UmsUser> register(@Validated @RequestBody UmsUserDto umsUserDto) {
        return umsUserService.register(umsUserDto);
    }

    @ApiOperation("用户登录")
    @PostMapping( "login")
    public ResponseVo<Map<String, String>> login(@Validated @RequestBody UmsUserLoginDto umsUserLoginDto) {
        return umsUserService.login(umsUserLoginDto);
    }

    @ApiOperation("刷新token")
    @GetMapping("refreshToken")
    public ResponseVo<Map<String, String>> refreshToken(HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        return umsUserService.refreshToken(token);
    }

    @ApiOperation("获取当前登录用户信息")
    @GetMapping("info")
    public ResponseVo<Map<String, Object>> getUserInfo(Principal principal) {
        if(Objects.isNull(principal)){
            return ResponseVo.unauthorized(null);
        }
        String username = principal.getName();
        return umsUserService.getUserInfo(username);
    }

    @ApiOperation("登出功能")
    @PostMapping("/logout")
    public ResponseVo<String> logout(Principal principal) {
        // 暂时没处理
        if(Objects.isNull(principal)){
            return ResponseVo.unauthorized(null);
        }
        return umsUserService.logout(principal.getName());
    }

    @ApiOperation("根据用户名或姓名分页获取用户列表")
    @GetMapping("/list")
    public ResponseVo<CommonPage<UmsUser>> list(@RequestParam(value = "keyword", required = false) String keyword,
                                                @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return umsUserService.list(keyword, pageSize, pageNum);
    }

    @ApiOperation("获取指定用户信息")
    @GetMapping("{userId}")
    public ResponseVo<UmsUser> getItem(@PathVariable("userId") Long userId) {
        return ResponseVo.success(umsUserService.getById(userId));
    }

    @ApiOperation("修改指定用户信息")
    @PostMapping("update/{userId}")
    public ResponseVo<String> update(@PathVariable("userId") Long userId,
                                     @RequestBody UmsUser user) {
        return umsUserService.update(userId, user);
    }

    @ApiOperation("修改指定用户密码")
    @PostMapping("updatePassword")
    public ResponseVo<String> updatePassword(@Validated @RequestBody UpdateUserPasswordDto updateUserPasswordDto) {
        return umsUserService.updatePassword(updateUserPasswordDto);
    }

    @ApiOperation("删除指定用户信息")
    @PostMapping("delete/{userId}")
    public ResponseVo<String> delete(@PathVariable("userId") Long userId) {
        return umsUserService.delete(userId);
    }

    @ApiOperation("修改帐号状态")
    @PostMapping("updateStatus/{userId}")
    public ResponseVo<String> updateStatus(@PathVariable("userId") Long userId,
                                           @RequestParam("status") Integer status) {
        UmsUser user = new UmsUser();
        user.setStatus(status);
        return umsUserService.update(userId, user);
    }

    @ApiOperation("给用户分配角色")
    @PostMapping("role/update")
    public ResponseVo<Integer> updateRole(@RequestParam("userId") Long userId,
                                          @RequestParam("roleIds") List<Long> roleIds) {
        return umsUserService.updateRole(userId, roleIds);
    }

    @ApiOperation("获取指定用户的角色")
    @GetMapping( "role/{userId}")
    public ResponseVo<List<UmsRole>> getRoleList(@PathVariable("userId") Long userId) {
        return umsUserService.getRoleList(userId);
    }
}

